package net.tooeye.easymes.activities;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Window;
import android.widget.TextView;



import net.tooeye.easyplus.R;

public class SplashActivity extends AppCompatActivity {

    private TextView textView;

    Handler mHandler;

    {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                textView.setText((String)msg.obj);
                startLogin();
            }
        };
    }

    private void startLogin() {
        this.startActivity(new Intent(this,LoginActivity.class));
        this.finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_splash);
        ActionBar bar = getSupportActionBar();
        if (bar != null){
            bar.hide();
        }
        init();
    }

    protected void onDestroy() {
        super.onDestroy();
        mHandler.removeCallbacksAndMessages(null);
    }

    private void init() {
        textView =  findViewById(R.id.textViewInfo);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                    SharedPreferences sp  = getSharedPreferences("easy_plus", Context.MODE_PRIVATE);
                    String host = sp.getString("host","");
                    Log.d("song",host.length()+"");
                    Message message = new Message();
                    message.what = 1;
                    if (!TextUtils.isEmpty(host)){
                        message.obj = "自动登录";
                    }else{
                        message.obj = "跳转到登录页面";
                    }
                    mHandler.sendMessage(message);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

}
